<?php
	// TODO Has-many linkage
    
    include_once('ajax.init.php');
    
    include_once('optionsModel.php');
    // include_once('mainView.php');
    
    $optionsModel = new OptionsModel();
    
    // Get the parameters:
	$type     = $_GET['type']; 
	$optionID = $_GET['optionID'];
    $counter  = $_GET['manycounter'];
    
    $option   = $optionsModel->getOption($optionID);
    $name     = 'option_'.$optionID.'_m_'.$counter;
    
    echo '<li>';
    switch($type) {
        case 'textarea':
        {
            // Textarea
            echo '<textarea rows="6" cols="40" name="'.$name.'">'.$option['value'].'</textarea>';
            break;
        }
        case 'category' :
        {							
            // Category
            // Show the contents of this category:
            $sql = 'SELECT `id`,`name` FROM `'.$dbprefix.'items` WHERE `id_category`='.$option['value'].' ORDER BY `name` ASC;';
            $categoryResult = mq($sql);
            if(mysql_num_rows($categoryResult)==0) {
                echo '<select name="'.$name.'" disabled="disabled"><option value="0">Category is empty</option></select>';
            } else {
                echo '<select name="'.$name.'">';
                while($category = ma($categoryResult)) {
                    echo '<option value="'.$category['id'].'"';
                    if($category['id']==$option['value']) { echo ' selected="selected"'; }
                    echo '>'.$category['name'].'</option>';
                }
                echo '</select>';
            }
            break;
        }
        case 'text' :
        case 'url' :
        case 'email':
        default:
        {
            // Default text input
            echo '<input class="text" type="text" name="'.$name.'" value="'.$option['value'].'" />';
            break;
        }
    }
    echo '</li>';
    
    
	// echo '<li>'.$type.' '.$optionID.' <em>todo: coming soon!</em></li>';
	/*
	$result = mq('SELECT A.*, B.`hasmany` FROM `'.$dbprefix.'options` A, `'.$dbprefix.'datatype_option` B WHERE B.`id_datatype`='.$id_datatype.' AND B.`id_option` = A.`id`;');
			while($option = ma($result)) {
				if(empty($option['description'])) { $option['description']=$option['name']; }
				if($action=='edit' && $option['type']!='category') {
					$option['value'] = sr('SELECT `value` FROM `'.$dbprefix.'values` WHERE `id_option`='.$option['id'].' AND `id_item`='.$info['id'].';');
				}
				echo '<div>';
				echo '<label>'.$option['description'].':</label>';
				if($option['hasmany']==1) { echo '<ol><li>'; }
				switch($option['type']) {
					case 'textarea':
						{
							// Textarea
							echo '<textarea rows="6" cols="40" name="option_'.$option['id'].'">'.$option['value'].'</textarea>';
							break;
						}
					case 'category' :
						{							
							// Category
							// Show the contents of this category:
							$sql = 'SELECT `id`,`name` FROM `'.$dbprefix.'items` WHERE `id_category`='.$option['value'].' ORDER BY `name` ASC;';
							$categoryResult = mq($sql);
							if(mysql_num_rows($categoryResult)==0) {
								echo '<select name="option_'.$option['id'].'" disabled="disabled"><option value="0">Category is empty</option></select>';
							} else {
								echo '<select name="option_'.$option['id'].'">';
								while($category = ma($categoryResult)) {
									echo '<option value="'.$category['id'].'"';
									if($category['id']==$option['value']) { echo ' selected="selected"'; }
									echo '>'.$category['name'].'</option>';
								}
								echo '</select>';
							}
							break;
						}
					case 'text' :
					case 'url' :
					case 'email':
					default:
						{
							// Default text input
							echo '<input class="text" type="text" name="option_'.$option['id'].'" value="'.$option['value'].'" />';
							break;
						}
				}
				if($option['hasmany']==1) {
					echo ' <a href="#" class="addAnother">Add another</a></li></ol>';
				}
				echo '</div>';
			}	
	*/
	
?>